package com.kaisheng.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaisheng.entity.Files;
import com.kaisheng.entity.User;
import com.kaisheng.util.DBHelp;
import com.kaisheng.util.RowMapper;

public class FilesDao {
	DBHelp<Files> db = new DBHelp<Files>();
	public List<Files> findByProjectId(String project_id) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t_files.id AS files_id,t_files.name AS files_name,t_files.user_id AS files_userid,t_files.project_id AS files_projectid,");
		sql.append("t_files.createtime AS files_createtime,t_files.num AS files_num,t_files.size AS files_size,t_user.name AS user_name FROM t_files ");
		sql.append("LEFT JOIN t_user ON t_files.user_id = t_user.id WHERE t_files.project_id = ?");
		return db.executeQueryForList(sql.toString(), new FilesRowMapper(), project_id);
	}
	public void save(Files files) {
		String sql = "INSERT INTO t_files(id,`name`,user_id,project_id,createtime) VALUES (?,?,?,?,?)";
		db.executeSQL(sql, files.getId(),files.getName(),files.getUser_id(),files.getProject_id(),files.getCreatetime());
	}
	

	public void update(Files files,String filesize,int n) {
		String sql = "UPDATE t_files SET num = ?,size = ? where id = ?";
		db.executeSQL(sql, files.getNum()+n,filesize,files.getId());
		
	}
	
	public Files findByFilesId(String files_id) {
		String sql = "SELECT id,`name`,user_id,project_id,createtime,num,size FROM t_files WHERE id = ?";
		return db.executeQueryForObject(sql, FilesA, files_id);
	}

	public boolean del(String files_id) {
		String sql = "DELETE FROM  t_file WHERE files_id = ?";
		String sql2 = "DELETE FROM  t_files WHERE id = ?";
		db.executeSQL(sql, files_id);
		return db.executeSQL(sql2, files_id);
		
	}
	
	public class FilesRowMapper implements RowMapper<Files>{

		public Files mapRow(ResultSet rs) throws SQLException {
			Files files = new Files();
			files.setId(rs.getString("files_id"));
			files.setName(rs.getString("files_name"));
			files.setUser_id(rs.getString("files_userid"));
			files.setProject_id(rs.getString("files_projectid"));
			files.setCreatetime(rs.getString("files_createtime"));
			files.setNum(rs.getInt("files_num"));
			files.setSize(rs.getString("files_size"));
			
			User user = new User();
			user.setId(files.getUser_id());
			user.setName(rs.getString("user_name"));
			
			files.setUser(user);
			
			return files;
		}
		
	}
	RowMapper<Files> FilesA = new  RowMapper<Files>(){

		public Files mapRow(ResultSet rs) throws SQLException {
			Files files = new Files();
			files.setId(rs.getString("id"));
			files.setName(rs.getString("name"));
			files.setUser_id(rs.getString("user_id"));
			files.setProject_id(rs.getString("project_id"));
			files.setCreatetime(rs.getString("createtime"));
			files.setNum(rs.getInt("num"));
			files.setSize(rs.getString("size"));
			
			return files;
		}
		
	};
	


	
	
	
}
